using _RobotInspection.Attributes;
using _RobotInspection.DTO;
using _RobotInspection.Entities;
using _RobotInspection.Interfaces;
using _RobotInspection.Models;
using _RobotInspection.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace _RobotInspection.Controllers
{
    /// <summary>
    /// 用户控制层
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class UserController : ControllerBase
    {
        private readonly IUserService _service;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        public UserController(IUserService service)
        {
            _service = service;
        }

        [HttpGet]
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult<ApiResponse<List<UserDto>>>> GetUsers()
        {
            var result = await _service.GetUsersAsync();
            return new OkObjectResult(result);
        }

       /// <summary>
       /// 分页获取用户列表
       /// </summary>
       /// <param>查询条件</param>
       /// <returns name="result">用户列表</returns>
       [HttpGet]
        public async Task<ActionResult<ApiResponse<UserPagedResultDto>>> GetUsersPaged([FromQuery] UserPagedQueryDto queryDto)
        {
            var result = await _service.GetUsersPagedAsync(queryDto);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 根据ID获取用户信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>用户信息</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResponse<UserDto>>> GetUserById([FromRoute] long id)
        {
            var result = await _service.GetUserByIdAsync(id);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 根据用户名获取用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        [HttpGet("{userName}")]
        public async Task<ActionResult<ApiResponse<UserDto>>> GetUserByUserName([FromRoute] string userName)
        {
            var result = await _service.GetUserByUserNameAsync(userName);
            return new OkObjectResult(result);
        }

      

      

        /// <summary>
        /// 根据id修改用户的用户名
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult<ApiResponse<UserDto>>> UpdateUserName(long id, string userName)
        {
            var result = await _service.UpdateUserNameAsync(id, userName);
            return result;
        }

        /// <summary>
        /// 根据id修改用户的密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult<ApiResponse<UserDto>>> UpdateUserPassword(long id, string userPassword)
        {
            var result = await _service.UpdateUserPasswordAsync(id, userPassword);
            return result;
        }

        /// <summary>
        /// 根据id修改用户的权限
        /// </summary>
        /// <param name="id"></param>
        /// <param name="permission"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult<ApiResponse<UserDto>>> UpdateUserPermission(long id, byte permission)
        {
            var result = await _service.UpdateUserPermissionAsync(id, permission);
            return result;
        }

        /// <summary>
        /// 根据id修改用户的状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult<ApiResponse<UserDto>>> UpdateUserStatus(long id, byte status)
        {
            var result = await _service.UpdateUserStatusAsync(id, status);
            return result;
        }

        /// <summary>
        /// 根据id删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResponse<UserDto?>>> DeleteUser([FromRoute] long id)
        {
            var result = await _service.DeleteUserAsync(id);
            return result;
        }
        /// <summary>
        /// 设备绑定用户
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="EquipmentId"></param>
        /// <returns></returns>
        [HttpPost("equipment-binding")]
        [ApiStatus(ApiStatusEnum.Testing)]
        public async Task<ActionResult<ApiResponse<bool>>> EquipmentBindingUser(int UserId, List<long> EquipmentId)
        {
            return await _service.EquipmentBindingUser(UserId, EquipmentId);

        }
    }
}